package com.arm.service.impl;

import com.arm.dao.AdminUserMapper;
import com.arm.module.AdminUser;
import com.arm.service.AdminUserService;
import com.arm.service.BaseService;
import com.arm.tools.MD5Tools;
import com.arm.tools.myRunTime.MyRunTimeExcption;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Andrew  on 2017/1/2.
 */
@Service
public class AdminUserServiceImpl extends BaseService implements AdminUserService {
    @Autowired
    AdminUserMapper adminUserMapper;


    @Override
    public AdminUser login(Map<String, String> map) {
        String passWord = map.get("passWord");
        String userName = map.get("userName");
        map.put("passWord", MD5Tools.MD5(passWord));
        map.put("userName", userName);
        map.put("status", String.valueOf(AdminUser.STATUS_VALID));
        return adminUserMapper.selectAdminUser(map);
    }

    @Override
    public List<AdminUser> getUserList(Map<String, String> map) {
        return adminUserMapper.selectAdminUserList(map);
    }

    @Override
    public AdminUser getUser(Map<String, String> map) {
        if (!map.containsKey("tid")) {
            throw new MyRunTimeExcption("tid is null");
        }
        return adminUserMapper.selectAdminUser(map);

    }

    @Override
    public AdminUser selectAdminById(Map<String, String> map) {
        return adminUserMapper.selectAdminById(map);
    }

    @Override
    public void modifyUser(AdminUser adminUser) {

        if (adminUser.getTid() == null) {
            adminUser.setCreateTime(new Date());
            adminUser.setPassWord(MD5Tools.MD5("123456"));
            adminUserMapper.insertAdminUser(adminUser);
            return;
        }
        adminUserMapper.updateAdminUserByPrimaryKeySelective(adminUser);
    }
}
